//---------------------------------------------------------------------------


#include "Boards.h"
Boards::Boards(){
}
	void Boards::initArea(){
		for (int i=0;i<=7;i++){
			for (int j=0;j<=7;j++){
				area[i][j] = 0;
			}
		}
	}
	void Boards::initCoin(){
		myCoin[0].setY(0);
		myCoin[0].setX(1);
		myCoin[1].setY(0);
		myCoin[1].setX(3);
		myCoin[2].setY(0);
		myCoin[2].setX(5);
		myCoin[3].setY(0);
		myCoin[3].setX(7);
		myCoin[4].setY(1);
		myCoin[4].setX(0);
		myCoin[5].setY(1);
		myCoin[5].setX(2);
		myCoin[6].setY(1);
		myCoin[6].setX(4);
		myCoin[7].setY(1);
		myCoin[7].setX(6);
		myCoin[8].setY(6);
		myCoin[8].setX(1);
		myCoin[9].setY(6);
		myCoin[9].setX(3);
		myCoin[10].setY(6);
		myCoin[10].setX(5);
		myCoin[11].setY(6);
		myCoin[11].setX(7);
		myCoin[12].setY(7);
		myCoin[12].setX(0);
		myCoin[13].setY(7);
		myCoin[13].setX(2);
		myCoin[14].setY(7);
		myCoin[14].setX(4);
		myCoin[15].setY(7);
		myCoin[15].setX(6);
		for (int k =0; k<=15; k++){
			myCoin[k].setNumber(k+1);
			myCoin[k].setStatus(1);			
			myCoin[k].setAlive(1);			
		}
	}
	void Boards::fillArea(){
		initArea();
		for (int i=0;i<=15;i++){
			area[myCoin[i].getY()][myCoin[i].getX()] = myCoin[i].getNumber();
			
		}
	}
	void Boards::printCoin(){
		int virtualArea[8][8];	
		for (int i=0;i<=7;i++){
			for (int j=0;j<=7;j++){
				virtualArea[i][j] = 0;
			}
		}
		for (int i=0;i<=15;i++){
			if (myCoin[i].getAlive()==1){
				virtualArea[myCoin[i].getY()][myCoin[i].getX()] = myCoin[i].getNumber();
			}
		}
		cout<<"# # # # # # # # # #"<<endl;
		for (int i=0;i<=7;i++){
			cout<<endl<<"| ";
			for (int j=0;j<=7;j++){
				switch (virtualArea[i][j]){
					case 0:
						cout<<"- ";
						break;
					
					case 9:
						cout<<"a ";
						break;
					case 10:
						cout<<"b ";
						break;
					case 11:
						cout<<"c ";
						break;
					case 12:
						cout<<"d ";
						break;
					case 13:
						cout<<"e ";
						break;
					case 14:
						cout<<"f ";
						break;
					case 15:
						cout<<"g ";
						break;
					case 16:
						cout<<"h ";
						break;
					default:
						cout<<virtualArea[i][j]<<" ";				
						break;
				}
			}
			cout<<"|"<<endl;
		}
		cout<<endl<<"# # # # # # # # # #"<<endl;
	}
	void Boards::printArea(){
		cout<<"# # # # # # # # # #"<<endl;
		for (int i=0;i<=7;i++){
			cout<<endl<<"| ";
			for (int j=0;j<=7;j++){
				switch (area[i][j]){
					case 0:
						cout<<"- ";
						break;
					
					case 9:
						cout<<"a ";
						break;
					case 10:
						cout<<"b ";
						break;
					case 11:
						cout<<"c ";
						break;
					case 12:
						cout<<"d ";
						break;
					case 13:
						cout<<"e ";
						break;
					case 14:
						cout<<"f ";
						break;
					case 15:
						cout<<"g ";
						break;
					case 16:
						cout<<"h ";
						break;
					default:
						cout<<area[i][j]<<" ";				
						break;
				}
			}
			cout<<"|"<<endl;
		}
		cout<<endl<<"# # # # # # # # # #"<<endl;

	}
	
	void Boards::playerTurn(){
		 if (battleMode == 1){
			if(turn==1){
				turn = 2;
			}
			else if (turn==2) {
                            turn = 1;
				 }
		 }
		 else if (battleMode = 2){
			if(turn==1){
				turn = 3;
			}
			else if (turn==3) {
                            turn = 1;
				 }
	//
		 }
		 else if (battleMode = 3){
	//
			if(turn==3){
				turn = 4;
			}
			else if (turn==4) {
                            turn = 3;
				 }
		 }
	}
	void Boards::playGame(){
		initGame();

        gameLoop();
	}
	void Boards::initGame(){
		turn = 1;
		battleMode = 2;
		endGame = false;
		enemyFront = false;
		pauseGame = false;

		initArea();
		initCoin();
		fillArea();
		printArea();

	}
	void Boards::gameLoop(){
		while (!endGame){
			moveCoin();
			refresh();
			//fillArea();
			printArea();
			printCoin();
		}

	}
	void Boards::moveCoin(){
		// int input;
		// bool valid = false;
		int moveTo=0;
		
			if ((isForceEnemy()) && (!enemyFront)){ // jika harus memakan lawan dan baru pertama kali
				enemyFront = true; // penanda bahwa harus memakan lawan
				currentCoin = 9999; // penanda bahwa baru pertama kali harus memakan lawan
				cout<<endl<<"harus makan!!!";
			}
			if ((turn ==1)||(turn ==2)){
				playerMovement(&moveTo);
			}
			else{
				comAIMovement(&moveTo);
			}
			
			if (enemyFront){
				passCoinTo(moveTo);
				if (!stillForceEnemy(currentCoin)){ // jika sudah tidak bisa memakan lawan lagi
					playerTurn(); // ganti pemain
					enemyFront = false; // penanda dimatikan agar tidak memakan lawan lagi
				}
				else{
					cout<<endl<<"harus makan lagi WOIII ga ganti TURN!!!";
				
				}
			}
			else{
				moveCoinTo(moveTo);
				playerTurn(); // ganti pemain
			}
		
		// cout<<endl<<"masukkan kode coin = ";
		// cin>>input;
		// if ((turn==2)&& (input>0) && (input<9)){
			// valid = true;
		// }
		// else if((turn==1) &&(input>8) && (input<17)){
			// valid = true;
		// }
		// else {
			// cout<<"salah coin mas"<<endl;
		// }
			
			
		// if (valid){
			// if (isForceEnemy()){
				// forceEnemy = true;
				// enemyBlast(input);
			// }
			// else{
				// forceEnemy = false;
					// cout<<endl<<"melangkah ke = ";
					// cin>>moveTo;
					// if ((moveTo>0)&&(moveTo<5)) {
							// if ((turn==2) && ((getCoin(input).getStatus()==2) || ((getCoin(input).getStatus()==1) &&((moveTo==3)||(moveTo==4))))){
								// moveCoinTo(input,moveTo);
						   // }
							// else if ((turn==1)&&((getCoin(input).getStatus()==2) || ((getCoin(input).getStatus()==1) &&((moveTo==1)||(moveTo==2))))){
								// moveCoinTo(input,moveTo);
						   // }
					   // else{
							// cout<<endl<<"salah arah, anda bukan Raja"<<endl;
					   // }
					  // }
			
			// }
					
		// }
	}
	void Boards::enemyBlast(int coin){
		int moveTo;
		if(checkArea(coin)!=0){
			cout<<endl<<"makan coin! Mempass lawan ke = ";
			cin>>moveTo;
			if (isPassAble(coin,moveTo)){
				switch(moveTo){
					case 1:
						passLeftUp(coin);
						break;
					case 2:
						passRightUp(coin);
						break;
					case 3:
						passRightDown(coin);
						break;
					case 4:
						passLeftDown(coin);
						break;								
				}
				if ((getCoin(coin).getY()==7) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				if (checkArea(coin)!=0){
					enemyBlast(coin);
					cout<<endl<<"masih ada lawan tuh";
				}
				else{
					//playerTurn();
				}

			}
			else{
				cout<<endl<<"Anda harus mempass lawan... "<<moveTo;
			}
		}
		else{
			cout<<endl<<"coin yang dipilih bukan coin yang sedang berhadapan dengan lawan";
		}
	}
	void Boards::moveCoinTo(int moveTo){
		switch(moveTo){
			case 1:
				coinMoveLeftUp(currentCoin);
				break;
			case 2:
				coinMoveRightUp(currentCoin);
				break;
			case 3:
				coinMoveRightDown(currentCoin);
				break;
			case 4:
				coinMoveLeftDown(currentCoin);
				break;
		}
	}
	void Boards::passCoinTo(int moveTo){
		switch(moveTo){
			case 1:
				passLeftUp(currentCoin);
				break;
			case 2:
				passRightUp(currentCoin);
				break;
			case 3:
				passRightDown(currentCoin);
				break;
			case 4:
				passLeftDown(currentCoin);
				break;
		}
	}
	void Boards::coinMoveLeftUp(int coin){
		//cout<<endl<<"posisi koin di "<<getCoin(coin).getX()<<","<<getCoin(coin).getY();
		if ((getCoin(coin).getX()>0) && (getCoin(coin).getY()>0)){ // jika posisinya tidak di paling kiri atau di paling atas papan
			if (!existCoinLeftUp(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()-1][getCoin(coin).getX()-1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()-1);
				myCoin[coin-1].setY(getCoin(coin).getY()-1);
					cout<<"melangkah dengan pasti";
				if ((getCoin(coin).getY()==0) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
				cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				//playerTurn();
			}
			else{
				//
				cout<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			cout<<"nabrak dinding";
		}
	}
	void Boards::coinMoveLeftDown(int coin){
		if ((getCoin(coin).getX()>0) && (getCoin(coin).getY()<7)){ // jika posisinya tidak di paling kiri atau di paling bawah papan
			if (!existCoinLeftDown(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()+1][getCoin(coin).getX()-1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()-1);
				myCoin[coin-1].setY(getCoin(coin).getY()+1);
				if ((getCoin(coin).getY()==7) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;

				}
				cout<<"melangkah dengan pasti";
				//playerTurn();
			}
			else{
				//
				cout<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			cout<<"nabrak dinding";
		}
	}
	void Boards::coinMoveRightUp(int coin){
		if ((getCoin(coin).getX()<7) && (getCoin(coin).getY()>0)){ // jika posisinya tidak di paling kanan atau di paling atas papan
			if (!existCoinRightUp(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()-1][getCoin(coin).getX()+1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()+1);
				myCoin[coin-1].setY(getCoin(coin).getY()-1);
				if ((getCoin(coin).getY()==0) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				cout<<endl<<"melangkah dengan pasti";
				//playerTurn();
			}
			else{
				//
				cout<<endl<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			cout<<endl<<"nabrak dinding";
		}
	
	}
	void Boards::coinMoveRightDown(int coin){

		if ((getCoin(coin).getX()<7) && (getCoin(coin).getY()<7)){ // jika posisinya tidak di paling kanan atau di paling bawah papan
			if (!existCoinRightDown(coin)){
				area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
				area[getCoin(coin).getY()+1][getCoin(coin).getX()+1] = getCoin(coin).getNumber();
				myCoin[coin-1].setX(getCoin(coin).getX()+1);
				myCoin[coin-1].setY(getCoin(coin).getY()+1);
				if ((getCoin(coin).getY()==7) && (getCoin(coin).getStatus()==1)){
					myCoin[coin-1].setStatus(2);
					cout<<endl<<"Anda menjadi Raja"<<endl;
				}
				cout<<endl<<"melangkah dengan pasti";
				//playerTurn();
			}
			else{
				//
				cout<<endl<<"ada Coin lain nih... makan ga ya??";
			}
		}
		else{
			cout<<endl<<"nabrak dinding";
		}
	
	}
	bool Boards::existCoinLeftUp(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()-1][getCoin(coin).getX()-1]==0){
			empty = false;
		}
		else{
			cout<<endl<<"ada coin di kiri atas";
		}
		return empty;
	}
	bool Boards::existCoinLeftDown(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()+1][getCoin(coin).getX()-1]==0){
			empty = false;
		}
		else{
			cout<<endl<<"ada coin di kiri bawah";
		}
		return empty;
	
	}
	bool Boards::existCoinRightUp(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()-1][getCoin(coin).getX()+1]==0){
			empty = false;
		}
		else{
			cout<<endl<<"ada coin di kanan atas";
		}
		return empty;
	
	}
	bool Boards::existCoinRightDown(int coin){
		bool empty = true;
		if (area[getCoin(coin).getY()+1][getCoin(coin).getX()+1]==0){
			empty = false;
		}
		else{
			cout<<endl<<"ada coin di kanan bawah";
		}
		return empty;
	
	}
	bool Boards::passLeftUp(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()-2][getCoin(coin).getX()-2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()-1), getCoin(coin).getY()-1)-1].setAlive(0);
		area[getCoin(coin).getY()-1][getCoin(coin).getX()-1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()-2);
		myCoin[coin-1].setY(getCoin(coin).getY()-2);
		cout<<endl<<"melangkahi lawan ke kiri atas";
	}
	bool Boards::passRightUp(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()-2][getCoin(coin).getX()+2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()+1), getCoin(coin).getY()-1)-1].setAlive(0);
		area[getCoin(coin).getY()-1][getCoin(coin).getX()+1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()+2);
		myCoin[coin-1].setY(getCoin(coin).getY()-2);
		cout<<endl<<"melangkahi lawan ke kanan atas";
	
	}
	bool Boards::passRightDown(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()+2][getCoin(coin).getX()+2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()+1), getCoin(coin).getY()+1)-1].setAlive(0);
		area[getCoin(coin).getY()+1][getCoin(coin).getX()+1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()+2);
		myCoin[coin-1].setY(getCoin(coin).getY()+2);
		cout<<endl<<"melangkahi lawan ke kiri bawah";
	
	}
	bool Boards::passLeftDown(int coin){
		area[getCoin(coin).getY()][getCoin(coin).getX()] = 0;
		area[getCoin(coin).getY()+2][getCoin(coin).getX()-2] = getCoin(coin).getNumber();
		myCoin[getCoinNumberFromArea((getCoin(coin).getX()-1), getCoin(coin).getY()+1)-1].setAlive(0);
		area[getCoin(coin).getY()+1][getCoin(coin).getX()-1] = 0; // mati
		myCoin[coin-1].setX(getCoin(coin).getX()-2);
		myCoin[coin-1].setY(getCoin(coin).getY()+2);
		cout<<endl<<"melangkahi lawan ke kanan bawah";
	
	}
	bool Boards::isLeftSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getX()==0){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	bool Boards::isRightSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getX()==7){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	bool Boards::isUpSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getY()==0){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	bool Boards::isDownSide(int coin){
		bool stuck = false;
		if (getCoin(coin).getY()==7){ // jika posisinya tidak di paling kiri papan
			stuck = true;
		}
		return stuck;
	}
	void Boards::refresh(){
			system("PAUSE");
		system("cls");
	}
	Coin Boards::getCoin(int coin){
		return myCoin[coin-1];
	}
	int Boards::checkArea(int coin){
		int check = 0;
		if (((getCoin(coin).getX()-2)>=0) && ((getCoin(coin).getY()-2)>=0) && ((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
				if ((existCoinLeftUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()-1))) != getType(coin))){
					check = 1;
					cout<<endl<<"ada coin musuh di kiri atas";
				}
		}
		if (((getCoin(coin).getX()+2)<=7) && ((getCoin(coin).getY()-2)>=0)&&((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
				if ((existCoinRightUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()-1))) != getType(coin))){
					check = 2;
					cout<<endl<<"ada coin musuh di kanan atas";
				}
			
		}
		if (((getCoin(coin).getX()+2)<=7) && ((getCoin(coin).getY()+2)<=7)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
				if ((existCoinRightDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()+1))) != getType(coin))){
					check = 3;
					cout<<endl<<"ada coin musuh di kanan bawah";
				}
			
		}
		if (((getCoin(coin).getX()-2)>=0) && ((getCoin(coin).getY()+2)<=7)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
				if ((existCoinLeftDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()+1))) != getType(coin))){
					check = 4;
					cout<<endl<<"ada coin musuh di kiri bawah";
				}
			
		}
		cout<<"area = "<<check;
		return check;
	}
	int Boards::getType(int coin){
		if ((coin>=1) && (coin<=8)){
			return 2;
		}
		else {
			return 1;
		}
	}
	int Boards::getCoinNumberFromArea(int x, int y){
		return area[y][x];
	}
	bool Boards::isPassAble(int coin, int dir){
		bool check = false;
		if (((getCoin(coin).getX()-2)>=0) && ((getCoin(coin).getY()-2)>=0) && ((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
				if ((existCoinLeftUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()-1))) != getType(coin))){
					if (dir==1){
						check = true;
					}
				}
		}
		if (((getCoin(coin).getX()+2)<=7) && ((getCoin(coin).getY()-2)>=0)&&((getType(coin)==1)||(getCoin(coin).getStatus()==2))){
				if ((existCoinRightUp(coin)) && (area[getCoin(coin).getY()-2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()-1))) != getType(coin))){
					if (dir==2){
						check = true;
					}
				}
			
		}
		if (((getCoin(coin).getX()-2)>=0) && ((getCoin(coin).getY()+2)<=7)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
				if ((existCoinRightDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()+2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()+1),(getCoin(coin).getY()+1))) != getType(coin))){
					if (dir==3){
						check = true;
					}
				}
			
		}
		if (((getCoin(coin).getX()+2)<=7) && ((getCoin(coin).getY()+2)<=7)&&((getType(coin)==2)||(getCoin(coin).getStatus()==2))){
				if ((existCoinLeftDown(coin)) && (area[getCoin(coin).getY()+2][getCoin(coin).getX()-2]==0) && (getType(getCoinNumberFromArea((getCoin(coin).getX()-1),(getCoin(coin).getY()+1))) != getType(coin))){
					if (dir==4){
						check = true;
					}
				}
			
		}
		return check;
	}
	bool Boards::isForceEnemy(){
		bool face = false;
		cout<<endl<<"giliran pemain = "<<turn;
		if ((turn ==1) || (turn == 4)){ // posisi pemain 1 dan 4 di bawah
			for (int k=8; k<=15; k++){
				if (myCoin[k].getAlive()==1){
					if (checkArea(k+1)!=0){
						face = true;
						cout<<endl<<"ada coin yang berhadapan dengan lawan!!";
					}
					else{
						cout<<endl<<"gagal di check area";

					}
				}
			}
		
		}
		else {
			for (int k=0; k<=7; k++){ // posisi pemain 2 dan 3 di atas
				if (myCoin[k].getAlive()==1){
					if (checkArea(k+1)!=0){
						face = true;
						cout<<endl<<"ada coin yang berhadapan dengan lawan!!";
					}
					else{
						cout<<endl<<"gagal di check area";

					}
				}
			}
		
		}
		return face;
	}
	bool Boards::stillForceEnemy(int coin){
		if (checkArea(coin)!=0){
			cout<<endl<<"cek area false";
			return true;
		}
		else {
			cout<<endl<<"cek area true";
			return false;
		}
	}
	int Boards::frontKiller(){
		int firstCoin=9999;
		if ((turn ==1)||(turn ==4)){
			for (int i=0;i<=7;i++){
				if (getCoin(i+1).getAlive()==1){
					if (checkArea(i+1)!=0){
						if (firstCoin==9999){
							firstCoin = i+1;
						}
						else{
							if (getCoin(i+1).getY()>getCoin(firstCoin).getY()){
								firstCoin = i+1;
							}
						}
					}
				}
			}
		}
		else{
			for (int i=8;i<=15;i++){
				if (getCoin(i+1).getAlive()==1){
					if (checkArea(i+1)!=0){
						if (firstCoin==9999){
							firstCoin = i+1;
						}
						else{
							if (getCoin(i+1).getY()>getCoin(firstCoin).getY()){
								firstCoin = i+1;
							}
						}
					}
				}
			}
		
		}
		if (firstCoin==9999){
			cout<<endl<<"fungsi front Killer ERROR";
		}
		return firstCoin;
	}
	bool Boards::moreChanceToKill(){
		bool chance = 0;
		if (isPassAble(currentCoin,1)){
			chance += 1;
		}
		if (isPassAble(currentCoin,2)){
			chance += 1;
		}
		if (isPassAble(currentCoin,3)){
			chance += 1;
		}
		if (isPassAble(currentCoin,4)){
			chance += 1;
		}
		if (chance>1){
			return true;
		} else{
			return false;
		}
	}
	int Boards::frontPassKill(){
		int firstMove = checkArea(currentCoin);
		if (moreChanceToKill()){
			if (getType(currentCoin)==1){
				if (getCoin(currentCoin).getX()<4){
					if (isPassAble(currentCoin,1)){
						firstMove = 1;
					}
					else{
						if (isPassAble(currentCoin,2)){
							firstMove = 2;
						}
					}
				}
				else{
					if (isPassAble(currentCoin,2)){
						firstMove = 2;
					}
					else{
						if (isPassAble(currentCoin,1)){
							firstMove = 1;
						}
					}
				}
			}
			else{
				if (getCoin(currentCoin).getX()<4){
					if (isPassAble(currentCoin,3)){
						firstMove = 3;
					}
					else{
						if (isPassAble(currentCoin,4)){
							firstMove = 4;
						}
					}
				}
				else{
					if (isPassAble(currentCoin,4)){
						firstMove = 4;
					}
					else{
						if (isPassAble(currentCoin,3)){
							firstMove = 3;
						}
					}
				}
			
			}
		}
		return firstMove;
	}
	bool Boards::corner(int coin, int id){
		bool corner = false;
		switch(id){
			case 1:
				if ((getCoin(coin).getX()==0) || (getCoin(coin).getY()==0)){
					corner = true;
				}
				break;
			case 2:
				if ((getCoin(coin).getX()==7) || (getCoin(coin).getY()==0)){
					corner = true;
				}
				break;
			case 3:
				if ((getCoin(coin).getX()==7) || (getCoin(coin).getY()==7)){
					corner = true;
				}
				break;
			case 4:
				if ((getCoin(coin).getX()==0) || (getCoin(coin).getY()==7)){
					corner = true;
				}
				break;
		}
		return corner;
	}
	bool Boards::ableToMove(int coin){
		bool able = false;

			if (ableToMove(coin, 1)){
					able = true;
			cout<<endl<<"able to move 1";
			}
			else if (ableToMove(coin, 2)){
					able = true;
			cout<<endl<<"able to move 2";
				}
				else if (ableToMove(coin, 3)){
					able = true;
			cout<<endl<<"able to move 3";
				}
				else if (ableToMove(coin, 4)){
					able = true;
			cout<<endl<<"able to move 4";
				}
			if (able){
			cout<<endl<<"able to move (1)";
			}
		return able;	
	}
	bool Boards::ableToMove(int coin, int side){
		bool able = false;
		switch(side){
			case 1:
				if (((getType(coin)==1)||(getCoin(coin).getStatus()==2))&& (!existCoinLeftUp(coin))&& (!corner(coin, 1))){
					if (getType(coin)==1){
						cout<<endl<<"jebakan 1";
					}
					if (getCoin(coin).getStatus()==2){
						cout<<endl<<"jebakan 2";
					}
					if (!existCoinLeftUp(coin)){
						cout<<endl<<"jebakan 3";
					}
					if (!corner(coin, 1)){
						cout<<endl<<"jebakan 4";
					}
					able = true;
				}
				break;
			case 2:
				if (((getType(coin)==1)||(getCoin(coin).getStatus()==2))&& (!existCoinRightUp(coin))&& (!corner(coin, 2))){
					able = true;
				}
				break;
			case 3:
				if (((getType(coin)==2)||(getCoin(coin).getStatus()==2))&& (!existCoinRightDown(coin))&& (!corner(coin, 3))){
					able = true;
				}
				break;
			case 4:
				if (((getType(coin)==2)||(getCoin(coin).getStatus()==2))&& (!existCoinLeftDown(coin))&& (!corner(coin, 4))){
					able = true;
				}
				break;
		}
		return able;
	}
	int Boards::anyWhereCanMove(int coin){
		int able=0;
		
			if (ableToMove(coin, 1)){
					able = 1;
			}
			else if (ableToMove(coin, 2)){
					able = 2;
				}
				else if (ableToMove(coin, 3)){
					able = 3;
				}else if (ableToMove(coin, 4)){
					able = 4;
				}
		if (able==0){
			cout<<endl<<"error nih anywhere can move nya";
		}
		return able;		
	}
	int Boards::sideCoin(){
		int firstCoin=9999;
		if ((turn ==1)||(turn ==4)){
			for (int i=8;i<=15;i++){
				if ((getCoin((i+1)).getAlive()==1)&& (ableToMove(i+1))){
					cout<<endl<<"i+1 = "<<(i+1);
					if (getCoin((i+1)).getAlive()==1){
					
					cout<<endl<<"hidup "<<(i+1);
					}
						if (firstCoin==9999){
							firstCoin = (i+1);
							cout<<endl<<"firstCoin Gan = "<<firstCoin;
						}
						else{
							if (getCoin(i+1).getX()<4){
								if (getCoin(firstCoin).getX()>getCoin(i+1).getX()){
									if (ableToMove(i+1, 4)){ // mengutamakan ke kiri
										firstCoin = (i+1);
										cout<<endl<<"pergantian pemain nih Gan="<<firstCoin;
									}
									
								}
							}
							else{
								if (getCoin(firstCoin).getX()<getCoin(i+1).getX()){
									if (ableToMove(i+1, 3)){ // mengutamakan ke kanan
										firstCoin = (i+1);
										cout<<endl<<"mengutamakan ke kanan = "<<firstCoin;
									}
									
								}
							}
						}					
				
				}
			}
		}
		else{
			for (int i=0;i<=7;i++){
				if ((getCoin(i+1).getAlive()==1)&& (ableToMove(i+1))){
						if (firstCoin==9999){
							firstCoin = (i+1);
						}
						else{
							if (getCoin(i+1).getX()<4){
								if (getCoin(firstCoin).getX()>getCoin(i+1).getX()){
									if (ableToMove(i+1, 1)){ // mengutamakan ke kiri
										firstCoin = (i+1);
									}
									
								}
							}
							else{
								if (getCoin(firstCoin).getX()<getCoin(i+1).getX()){
									if (ableToMove(i+1, 2)){ // mengutamakan ke kiri
										firstCoin = (i+1);
									}
									
								}
							}
						}					

				}
			}
		
		}
		if (firstCoin==9999){
			cout<<endl<<"fungsi side coin error";
		}
		else{
			cout<<endl<<"hasil fungsi = "<<firstCoin;
		
		}
		return firstCoin;
	}
	int Boards::sideMove(){
		int firstMove = anyWhereCanMove(currentCoin);
		cout<<endl<<"first move = "<<firstMove;
		if (moreChanceToMove()){
			cout<<endl<<"banyak pilihan gerak";
			if (getType(currentCoin)==1){
				if (getCoin(currentCoin).getX()<4){
					if (ableToMove(currentCoin,1)){
						firstMove = 1;
						cout<<endl<<"bisa gerak ke 1";
					}
				}
				else{
					if (ableToMove(currentCoin,2)){
						firstMove = 2;
						cout<<endl<<"bisa gerak ke 2";
					}
				}
			}
			else{
				if (getCoin(currentCoin).getX()<4){
					if (ableToMove(currentCoin,4)){
						firstMove = 4;
						cout<<endl<<"bisa gerak ke 4";
					}
				}
				else{
					if (ableToMove(currentCoin,3)){
						firstMove = 3;
						cout<<endl<<"bisa gerak ke 3";
					}
				}
			
			}
		}
		return firstMove;
	}
	bool Boards::moreChanceToMove(){
		int able = 0;
		
			if (ableToMove(currentCoin,1)){
					able +=1;
			}
			if (ableToMove(currentCoin,2)){
					able +=1;
			}
			if (ableToMove(currentCoin,3)){
					able +=1;
			}
			if (ableToMove(currentCoin,4)){
					able +=1;
			}
		if (able>1){
			return true;
		}
		else{
			return false;
		}
	}
	void Boards::playerMovement(int *moveTo){
		int dir;
		if (enemyFront){ // jika harus memakan lawan
			if (currentCoin==9999){ // jika baru pertama kali berhadapan dengan lawan
				// cari koin yang bisa makan di paling depan
				cout<<endl<<"pilihan coin = ";
				cin>>currentCoin;
				cout<<endl<<"makan coin! Mempass lawan ke (1:kiri atas, 2: kanan atas, 3: kanan bawah, 4: kiri bawah) = ";
				cin>>dir;

			}
			else{ // jika harus memakan lawan lagi
				// cari arah makan yang paling di samping papan
				cout<<endl<<"makan coin lagi! Mempass lawan ke (1:kiri atas, 2: kanan atas, 3: kanan bawah, 4: kiri bawah) = ";
				cin>>dir;
			}
		}
		else{
			cout<<endl<<"pilihan coin = ";
			cin>>currentCoin;
			cout<<endl<<"langkahkan coin ke (1:kiri atas, 2: kanan atas, 3: kanan bawah, 4: kiri bawah) = ";
			cin>>dir;
		}
		(*moveTo) = dir;
				
	}
	void Boards::comAIMovement(int *moveTo){
		switch(comLevel){
			default:
				/*
				AI pada level satu merupakan Greedy by side. Algoritma ini mengutamakan keberadaan coin checkers agar selalu ada di samping papan
				*/
				if (enemyFront){ // jika harus memakan lawan
			 		if (currentCoin==9999){ // jika baru pertama kali berhadapan dengan lawan
						// cari koin yang bisa makan di paling depan
						currentCoin = frontKiller();
						cout<<endl<<"coin yang dipilih untuk membunuh= "<<currentCoin;
						(*moveTo) = frontPassKill();
						cout<<endl<<"langkah yang dipilih untuk membunuh= "<<(*moveTo);
					}
					else{ // jika harus memakan lawan lagi
						// cari arah makan yang paling di samping papan
						(*moveTo) = frontPassKill();
						cout<<endl<<"langkah yang dipilih untuk membunuh lagi= "<<(*moveTo);
					}
				}
				else{
					currentCoin = sideCoin();
					cout<<endl<<"coin yang dipilih untuk melangkah= "<<currentCoin;
					(*moveTo) = sideMove();
					cout<<endl<<"langkah yang dipilih sideMove= "<<sideMove();
					cout<<endl<<"langkah yang dipilih = "<<(*moveTo);
					
				}
				// coin = mostSideCoin(); // memilih koin yang prioritas utama berada di paling samping dan masih dapat bergerak ke samping
				// moveTo = mostSideGoal(); // memutuskan arah gerak untuk menuju ke paling samping papan
				
				break;
		}
	}
	
//---------------------------------------------------------------------------

